/**
 * Created by zhangxiaoyu on 16/12/09.
 */
angular.module('hyt.receiptCtrl',[])

  .controller('mailCtrl',function($scope,$filter,$ionicModal,Storage,ReceiptList,Mail,$ionicPopup,$ionicLoading,$state){
    $scope.showloading=true;
    var loginMsg=Storage.get('user');
    $scope.receiptSearch={
      parameter:{
        RECEIPT_STATUS:'1',
        WAYBILL_NO:'',
        WAYBILL_DATE:'',
        WAYBILL_DATE_TO:''
      },
      userMsg:{
        username:loginMsg.username,
        password:loginMsg.password,
        uuid:loginMsg.uuid
      }
    };
    ReceiptList.getReceiptData($scope.receiptSearch);
    $scope.$on('ReceiptList.Updated', function() {
      $scope.receiptData=ReceiptList.getResult();
      $scope.showloading=false;
    });
    //下拉刷新
    $scope.doRefresh=function(){
      ReceiptList.getReceiptData($scope.receiptSearch);
      $scope.$broadcast('scroll.refreshComplete');
    };
    //上拉加载更多
    $scope.loadMore=function(){
      ReceiptList.getMoreReceiptData($scope.receiptSearch);
      $scope.$broadcast('scroll.infiniteScrollComplete');
    };
    //下一页
    $scope.hasNextPage = function() {
      return ReceiptList.hasNextPage();
    };
    $scope.mail=function(id){
      var receiptId={
        id:id,
        userMsg:{
          username:loginMsg.username,
          password:loginMsg.password,
          uuid:loginMsg.uuid
        }
      };
      var confirmPopup = $ionicPopup.confirm({
        title:'是否寄出回单',
        okText:'是',
        cancelText:'否'
      });
      confirmPopup.then(function(res) {
        if(res){
          Mail.getMail(JSON.stringify(receiptId));
          $scope.$on('Mail.Updated', function() {
            var result=Mail.getResult();
            if (result.success==true){
              $ionicLoading.show({
                noBackdrop: true,
                template: result.rows,
                duration: 1500
              });
              ReceiptList.getReceiptData($scope.receiptSearch);
            }
          });
        }
        else {
          return false;
        }
      });
    };
    var t = new Date();
    var iToDay=t.getDate();
    var iToMon=t.getMonth();
    var iToYear=t.getFullYear();
    var iTDate=new Date(iToYear,iToMon,(iToDay-30));
    $scope.startDate=$filter('date')(iTDate,'yyyy-MM-dd');
    $scope.showStartDate = function() { //选择日期
      var optionsStart = {
        date: iTDate,
        mode: 'date',
        maxDate: new Date($scope.endDate) - 10000
      };
      datePicker.show(optionsStart, function(d) {
        if (!isNaN(d.getTime())) {  // valid date
          $scope.$apply(function () {
            iTDate=d;
            $scope.startDate = $filter('date')(iTDate,'yyyy-MM-dd');
          });
        }
      });
    };
    $scope.endDate=$filter('date')(new Date(),'yyyy-MM-dd');
    $scope.showEndDate = function(){
      console.log('EndDate');
      var optionsEnd = {
        date : new Date(),
        mode : 'date',
        minDate: new Date($scope.startDate) - 10000
      };
      datePicker.show(optionsEnd,function(d) {
        if (!isNaN(d.getTime())) {  // valid date
          $scope.$apply(function () {
            endTime=d;
            $scope.endDate = $filter('date')(endTime,'yyyy-MM-dd');
          });
        }
      });
    };
    $scope.searchReceipt=function(){
      $scope.receiptSearch.parameter.WAYBILL_DATE=$scope.startDate;
      $scope.receiptSearch.parameter.WAYBILL_DATE_TO=$scope.endDate;
      ReceiptList.getReceiptData($scope.receiptSearch);
      $scope.modal.hide();
    };
    $ionicModal.fromTemplateUrl('templates/tab-workbench/receipt/searchReceipt.html', {
      scope: $scope
    }).then(function(modal) {
      $scope.modal = modal;
    });
    $scope.goDetail=function(id,STATUS){
      Storage.set('wayId',id);
      $state.go('detailTab.detail',{STATUS:STATUS});
    };
  })
  .controller('receiveCtrl',function($scope,$filter,$ionicModal,Storage,ReceiptList,Receive,$ionicPopup,$ionicLoading,$state){
    $scope.showloading=true;
    var loginMsg=Storage.get('user');
    $scope.receiptSearch={
      parameter:{
        RECEIPT_STATUS:'2',
        WAYBILL_NO:'',
        WAYBILL_DATE:'',
        WAYBILL_DATE_TO:''
      },
      userMsg:{
        username:loginMsg.username,
        password:loginMsg.password,
        uuid:loginMsg.uuid
      }
    };
    ReceiptList.getReceiptData($scope.receiptSearch);
    $scope.$on('ReceiptList.Updated', function() {
      $scope.receiptData=ReceiptList.getResult();
      $scope.showloading=false;
    });
    //下拉刷新
    $scope.doRefresh=function(){
      ReceiptList.getReceiptData($scope.receiptSearch);
      $scope.$broadcast('scroll.refreshComplete');
    };
    //上拉加载更多
    $scope.loadMore=function(){
      ReceiptList.getMoreReceiptData($scope.receiptSearch);
      $scope.$broadcast('scroll.infiniteScrollComplete');
    };
    //下一页
    $scope.hasNextPage = function() {
      return ReceiptList.hasNextPage();
    };
    $scope.receive=function(id){
      var receiptId={
        id:id,
        userMsg:{
          username:loginMsg.username,
          password:loginMsg.password,
          uuid:loginMsg.uuid
        }
      };
      var confirmPopup = $ionicPopup.confirm({
        title:'是否接收回单',
        okText:'是',
        cancelText:'否'
      });
      confirmPopup.then(function(res) {
        if(res){
          Receive.getReceive(JSON.stringify(receiptId));
          $scope.$on('Receive.Updated', function() {
            var result=Receive.getResult();
            if (result.success==true){
              $ionicLoading.show({
                noBackdrop: true,
                template: result.rows,
                duration: 1500
              });
              ReceiptList.getReceiptData($scope.receiptSearch);
            }
          });
        }
        else {
          return false;
        }
      });
    };
    var t = new Date();
    var iToDay=t.getDate();
    var iToMon=t.getMonth();
    var iToYear=t.getFullYear();
    var iTDate=new Date(iToYear,iToMon,(iToDay-30));
    $scope.startDate=$filter('date')(iTDate,'yyyy-MM-dd');
    $scope.showStartDate = function() { //选择日期
      var optionsStart = {
        date: iTDate,
        mode: 'date',
        maxDate: new Date($scope.endDate) - 10000
      };
      datePicker.show(optionsStart, function(d) {
        if (!isNaN(d.getTime())) {  // valid date
          $scope.$apply(function () {
            iTDate=d;
            $scope.startDate = $filter('date')(iTDate,'yyyy-MM-dd');
          });
        }
      });
    };
    $scope.endDate=$filter('date')(new Date(),'yyyy-MM-dd');
    $scope.showEndDate = function(){
      console.log('EndDate');
      var optionsEnd = {
        date : new Date(),
        mode : 'date',
        minDate: new Date($scope.startDate) - 10000
      };
      datePicker.show(optionsEnd,function(d) {
        if (!isNaN(d.getTime())) {  // valid date
          $scope.$apply(function () {
            endTime=d;
            $scope.endDate = $filter('date')(endTime,'yyyy-MM-dd');
          });
        }
      });
    };
    $scope.searchReceipt=function(){
      $scope.receiptSearch.parameter.WAYBILL_DATE=$scope.startDate;
      $scope.receiptSearch.parameter.WAYBILL_DATE_TO=$scope.endDate;
      ReceiptList.getReceiptData($scope.receiptSearch);
      $scope.modal.hide();
    };
    $ionicModal.fromTemplateUrl('templates/tab-workbench/receipt/searchReceipt.html', {
      scope: $scope
    }).then(function(modal) {
      $scope.modal = modal;
    });
    $scope.goDetail=function(id,STATUS){
      Storage.set('wayId',id);
      $state.go('detailTab.detail',{STATUS:STATUS});
    };
  })
  .controller('grantCtrl',function($scope,$filter,$ionicModal,Storage,ReceiptList,$ionicPopup,Grant,$ionicLoading,$state){
    $scope.showloading=true;
    var loginMsg=Storage.get('user');
    $scope.receiptSearch={
      parameter:{
        RECEIPT_STATUS:'3',
        WAYBILL_NO:'',
        WAYBILL_DATE:'',
        WAYBILL_DATE_TO:''
      },
      userMsg:{
        username:loginMsg.username,
        password:loginMsg.password,
        uuid:loginMsg.uuid
      }
    };
    ReceiptList.getReceiptData($scope.receiptSearch);
    $scope.$on('ReceiptList.Updated', function() {
      $scope.receiptData=ReceiptList.getResult();
      $scope.showloading=false;
    });
    //下拉刷新
    $scope.doRefresh=function(){
      ReceiptList.getReceiptData($scope.receiptSearch);
      $scope.$broadcast('scroll.refreshComplete');
    };
    //上拉加载更多
    $scope.loadMore=function(){
      ReceiptList.getMoreReceiptData($scope.receiptSearch);
      $scope.$broadcast('scroll.infiniteScrollComplete');
    };
    //下一页
    $scope.hasNextPage = function() {
      return ReceiptList.hasNextPage();
    };
    $scope.grant=function(id){
      var receiptId={
        id:id,
        userMsg:{
          username:loginMsg.username,
          password:loginMsg.password,
          uuid:loginMsg.uuid
        }
      };
      var confirmPopup = $ionicPopup.confirm({
        title:'是否发放回单',
        okText:'是',
        cancelText:'否'
      });
      confirmPopup.then(function(res) {
        if(res){
          Grant.getGrant(JSON.stringify(receiptId));
          $scope.$on('Grant.Updated', function() {
            var result=Grant.getResult();
            if (result.success==true){
              $ionicLoading.show({
                noBackdrop: true,
                template: result.rows,
                duration: 1500
              });
              ReceiptList.getReceiptData($scope.receiptSearch);
            }
          });
        }
        else {
          return false;
        }
      });
    };
    var t = new Date();
    var iToDay=t.getDate();
    var iToMon=t.getMonth();
    var iToYear=t.getFullYear();
    var iTDate=new Date(iToYear,iToMon,(iToDay-30));
    $scope.startDate=$filter('date')(iTDate,'yyyy-MM-dd');
    $scope.showStartDate = function() { //选择日期
      var optionsStart = {
        date: iTDate,
        mode: 'date',
        maxDate: new Date($scope.endDate) - 10000
      };
      datePicker.show(optionsStart, function(d) {
        if (!isNaN(d.getTime())) {  // valid date
          $scope.$apply(function () {
            iTDate=d;
            $scope.startDate = $filter('date')(iTDate,'yyyy-MM-dd');
          });
        }
      });
    };
    $scope.endDate=$filter('date')(new Date(),'yyyy-MM-dd');
    $scope.showEndDate = function(){
      console.log('EndDate');
      var optionsEnd = {
        date : new Date(),
        mode : 'date',
        minDate: new Date($scope.startDate) - 10000
      };
      datePicker.show(optionsEnd,function(d) {
        if (!isNaN(d.getTime())) {  // valid date
          $scope.$apply(function () {
            endTime=d;
            $scope.endDate = $filter('date')(endTime,'yyyy-MM-dd');
          });
        }
      });
    };
    $scope.searchReceipt=function(){
      $scope.receiptSearch.parameter.WAYBILL_DATE=$scope.startDate;
      $scope.receiptSearch.parameter.WAYBILL_DATE_TO=$scope.endDate;
      ReceiptList.getReceiptData($scope.receiptSearch);
      $scope.modal.hide();
    };
    $ionicModal.fromTemplateUrl('templates/tab-workbench/receipt/searchReceipt.html', {
      scope: $scope
    }).then(function(modal) {
      $scope.modal = modal;
    });
    $scope.goDetail=function(id,STATUS){
      Storage.set('wayId',id);
      $state.go('detailTab.detail',{STATUS:STATUS});
    };
  });
